package com.yanqu.road.server.cmd.yrank.user;

import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.rank.yrank.UserYRank;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.AutoSendRewardProto;
import com.yanqu.road.pb.rank.YRankProto;
import com.yanqu.road.server.cmd.CrossUnionCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.activity.biaoju.CrossBiaoJuActivity;
import com.yanqu.road.server.manager.activity.biaoju.CrossBiaoJuMgr;
import com.yanqu.road.server.manager.yrank.CrossYRankMgr;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.YanQuMessageUtils;

import java.util.List;

@Cmd(code = CrossProtocol.C2_CROSS_YRANK_AUTO_SEND_USER_RANK, desc = "")
public class CrossYRankAutoSendUserRankCmd extends CrossUnionCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        YRankProto.YRankReqMsg reqMsg = YRankProto.YRankReqMsg.parseFrom(packet.getBytes());
        int conditionType = Integer.valueOf(reqMsg.getIdKey());
        if (conditionType == eGamePlayerEventType.BiaoJuExpRank.getValue()) {
            CrossBiaoJuActivity activity = CrossBiaoJuMgr.getActivity(reqMsg.getActivityId(), reqMsg.getGroupId());
            if (activity != null) {
                activity.autoSendUserRank(serverId);
            }
            return;
        }
        List<UserYRank> rankList = CrossYRankMgr.getSortUserRankList(reqMsg.getActivityId(), reqMsg.getGroupId(), reqMsg.getIdKey());

        AutoSendRewardProto.CrossUserRankMsg.Builder builder = AutoSendRewardProto.CrossUserRankMsg.newBuilder();
        builder.setActivityId(reqMsg.getActivityId());
        int rank = 1;
        for (UserYRank userYRank : rankList) {
            builder.addData(AutoSendRewardProto.UserRankMsg.newBuilder()
                    .setUserId(userYRank.getUserId())
                    .setRank(rank++)
                    .setConditionType(conditionType)
            );
        }

        YanQuMessage message = YanQuMessageUtils.buildMessage(GameProtocol.S_CROSS_ACTIVITY_AUTO_SEND_USER_REWARD, builder);
        MessageHelper.sendPacket(serverId, packet.getPlayerId(), message);
    }

    @Override
    public void sendData() {

    }
}
